package com.huitone.smspfm.oldclient.asclient;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;

/**
 * 新的旧平台序号过滤工具类
 * @author ozh
 *
 */
public class SeqSmsFilterQueue {

	private int maxSize = 100000/5;
	private int setNum = 6;
	private int setPoi = 0;
	private List<HashSet<String>> sets =new ArrayList<HashSet<String>>();
	
	public static SeqSmsFilterQueue filterQueue = new SeqSmsFilterQueue();
	
	private SeqSmsFilterQueue(){
		for(int i = 0; i < setNum; i++){
			sets.add(new HashSet<String>());
		}
	}
	
	public void add(String seq){
		boolean isFull = true;
		for(int i = 0; i < sets.size(); i++){
			HashSet<String> set = sets.get(i);
			int size = set.size();
			if(size < maxSize){
				set.add(seq);
				isFull = false;
				break;
			}
		}
		//数据已满
		if(isFull){
			sets.get(setPoi).clear();
			setPoi = (setPoi +1) %setNum;
		}
	}
	
	public synchronized boolean contains(String seq) {
		boolean result = true;
		for(int i = 0; i < sets.size(); i++){
			HashSet<String> set = sets.get(i);
			if(set.contains(seq)){
				result = true;
				break;
			} else {
				result = false;
			}
		}
		if(!result){
			add(seq);
		}
		
		return result;
	}
	
	public int getQuereSize(){
		int size = 0;
		for(int i = 0; i < sets.size(); i++){
			size += sets.get(i).size();
		}
		return size;
	}
	
	public static void main(String[] args){
		SeqSmsFilterQueue filterQuere = new SeqSmsFilterQueue();
		while (true) {
			String seq = "1111_";
			int ra = new Random().nextInt(30000);
			seq += ra;
			boolean result = filterQuere.contains(seq);
			if(result){
				System.out.println(seq+":已有重复: "+filterQuere.getQuereSize());
				
			}
			try {
				Thread.sleep(1);
			} catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
}
